package com.example.tkserver.dao;

import com.example.tkserver.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserDao {
    @Select("SELECT unix_timestamp(u.lastTime) lastTime,unix_timestamp(u.registerTime) registerTime,u.* " +
            "FROM user u WHERE openid = #{openId}")
    public User findByOpenId(String openId);

    @Insert("INSERT INTO user(openid,nickName,avatarUrl,name,gender,email,phone,password,college,classes,studentNumber,dorm,dormNumber,registerTime,lastTime) " +
            "VALUES(#{openId},#{nickName},#{avatarUrl},#{name},#{gender},#{email},#{phone},#{password},#{college}," +
            "#{classes},#{studentNumber},#{dorm},#{dormNumber},FROM_UNIXTIME(#{registerTime},'%Y-%m-%d %h:%i:%s'),FROM_UNIXTIME(#{lastTime},'%Y-%m-%d %h:%i:%s'))")
    @Options(useGeneratedKeys = true,keyProperty = "uid")
    public int saveUser(User user);

    @Update("UPDATE user SET scores = scores + #{scores} WHERE uid = #{uid}")
    public int addScores(int uid,int scores);

    @Update("UPDATE user SET " +
            "nickName = #{nickName}," +
            "avatarUrl = #{avatarUrl}," +
            "name = #{name}," +
            "gender = #{gender}," +
            "email = #{email}," +
            "phone = #{phone}," +
            "password = #{password}," +
            "college = #{college}," +
            "classes = #{classes}," +
            "studentNumber = #{studentNumber}," +
            "dorm = #{dorm}," +
            "dormNumber = #{dormNumber}," +
            "scores = #{scores} WHERE openId = #{openId} and uid = #{uid}")
    public int updateUser(User user);
}
